<?php
/**
 * Created by PhpStorm.
 * User: user
 * Date: 2018/12/11
 * Time: 11:19
 */
namespace app\house\controller;

use app\common\controller\Frontend;
use QL\QueryList;
use think\Cache;
use think\Config;
use app\house\model\Building;

class BuildingInfo extends Frontend{

    protected $noNeedLogin = ['index','collect','view','recode','allCollect'];
    protected  $url;
    protected  $model;
    //protected  $layout = 'default';
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->url = Config::get('collect_url');
        $this->model = model('BuildingInfo');
    }

    //采集房源开发商信息及小区各项信息
    public function index(){
        $url = $this->url;
        $hiddenValue = QueryList::get($url)->find('#iptstamp')->attr('value');
        $this->assign('iptstamp',$hiddenValue);
        return $this->fetch();
    }

    public function collect(){
        $url = $this->request->param('url');
        $build_id = $this->request->param('build_id');
        $requestUrl = $this->url.$url;
        $data = QueryList::get($requestUrl)->find('.info>.lbox >dd')->getString();
        $count = QueryList::get($requestUrl)->find('#ContentPlaceHolder1_dlBuildingList td')->count();
        $info = [
            "build_id"      => $build_id,
            "company"       =>  $data[0],
            "property"      =>  $data[1],
            "area"          =>  $data[2],
            "parrern"       =>  $data[3],
            "green_rate"    =>  $data[4],
            "build_ratio"   =>  $data[5],
            "update_num"    =>  $data[6],
            "update_rate"   =>  $data[7],
            "region"        =>  $data[8],
            "address"       =>  $data[9],
            "building_count"=>  $count
        ];
        $this->model->insert($info);
        echo 1;
    }


    public function recode($a)
    {
        $n	=	$this->nscaler($a);
        $c	=	$this->SetObjNum(strlen($a));
        $d	=	$this->SetObjNum(strlen($a));
        $n	=	(int)$n +	(int)$d;
        $cache_b = Cache::get('value');
        if($cache_b){
            $cache_value = $cache_b;
        }else{
            $cache_value  = QueryList::get($this->url)->find('#iptstamp')->attr('value');
            Cache::set('value',$cache_value,'10');
        }
        $b	=	$this->nscaler($cache_value);
        return $c."-".$n."-".$d."-".$b;
    }

    public function SetObjNum($n){
        $a = "";
        for($i=0;$i<$n;$i++){
            $a .= (int)($this->randFloat()*10);
        }
        return $a;
    }

    public function nscaler($a){
        $b="";
        $num = $a;
        $arr = array();
        $num = (string)$num;
        $len = strlen($num);
        for ($i = 0; $i < $len; $i++) {
            $arr[$i] = (int)$num[$i];
        }
        foreach ($arr as $item) {
            switch($item){
                case"0":$b .="0";break;
                case"1":$b .="2";break;
                case"2":$b .="5";break;
                case"3":$b .="8";break;
                case"4":$b .="6";break;
                case"5":$b .="1";break;
                case"6":$b .="3";break;
                case"7":$b .="4";break;
                case"8":$b .="9";break;
                case"9":$b .="7";break;
            }
        }
        return $b;
    }

    public function randFloat($min=0, $max=1){
        return $min + mt_rand()/mt_getrandmax() * ($max-$min);
    }


    /*
     * 采集页面
     */
    public function view(){

        return $this->view->fetch();
    }

    /*
     * 整站采集开发商信息
     *
     */
    public function allCollect(){
        $buildingModel = new Building();
        $page = $this->request->param('page',1);
        $count= $buildingModel->count();
        $data = $buildingModel->page($page,10)->select();
        $totalPage = ceil($count / 10);

        foreach ($data as $item) {
            $buildingInfo = $this->model->where('build_id',$item['build_id'])->find();
            $numString = $this->recode($item['build_id']);
            $requestUrl = $this->url.'/item/'.$numString;
            $data = QueryList::get($requestUrl)->find('.info>.lbox >dd')->getString();
            $countHouseNum = QueryList::get($requestUrl)->find('#ContentPlaceHolder1_dlBuildingList td')->count();
            if($buildingInfo){
                $info = [
                    "build_id"      => $item['build_id'],
                    "company"       =>  $data[0],
                    "property"      =>  $data[1],
                    "area"          =>  $data[2],
                    "parrern"       =>  $data[3],
                    "green_rate"    =>  $data[4],
                    "build_ratio"   =>  $data[5],
                    "update_num"    =>  $data[6],
                    "update_rate"   =>  $data[7],
                    "region"        =>  $data[8],
                    "address"       =>  $data[9],
                    "building_count"=>  $countHouseNum
                ];
                $this->model->save($info);
            }else{
                $info = [
                    "build_id"      => $item['build_id'],
                    "company"       =>  $data[0],
                    "property"      =>  $data[1],
                    "area"          =>  $data[2],
                    "parrern"       =>  $data[3],
                    "green_rate"    =>  $data[4],
                    "build_ratio"   =>  $data[5],
                    "update_num"    =>  $data[6],
                    "update_rate"   =>  $data[7],
                    "region"        =>  $data[8],
                    "address"       =>  $data[9],
                    "building_count"=>  $countHouseNum
                ];
                $this->model->insert($info);
            }

        }
        $progress = floor(($page / $totalPage) * 100) / $page;
        echo json_encode(['code'=>200,'progress'=>$progress,'totalPage'=>$totalPage,'msg'=>'success']);
    }

}